import { defineStore } from 'pinia'
import {ref} from "vue";
import {UserInfo} from '@/types/global.ts'
import {exitLogin, queryMenuTree} from "@/api";
import {message} from "ant-design-vue";
import {UploadQueueItem} from '@/types/files.ts'
 const useSystemStore = defineStore('useSystemStore', () => {
        const userInfo = ref<UserInfo|null>(null);
        const routerKey = ref([])
        const openKey = ref(['disk'])
        const menuTree = ref([])
        const collapsed = ref(false);
         const rememberMe = ref(false);
        // 上传队列
        const uploadQueue = ref<UploadQueueItem[]>([] as UploadQueueItem[]);
        const setUserInfo = (val:UserInfo)=>{
            userInfo.value = val;
        }
        const setRouterKey = (val:any)=>{
             routerKey.value = val;
        }
         const setOpenKey = (val:any)=>{
             openKey.value = val;
         }
         const setMenuTree = (val:any)=>{
             menuTree.value = val;
         }
         const setUploadQueue = (val:UploadQueueItem[])=>{
             uploadQueue.value = val;
         }
         const setRememberMe = (val:boolean)=>{
             rememberMe.value = val;
         }
         const refreshMenu = (router:any)=>{
             const modules = import.meta.glob('@/views/**/*.vue')
             const generateRouter = (menuList:any,tree:any,parentKey = [])=>{
                 if (!Array.isArray(menuList)) {
                     return menuList;
                 }

                 menuList.forEach((item:any)=>{
                     item.parentKey = parentKey;
                     item['meta'] = {
                         title:item.name,
                         icon:item.icon
                     }
                     if (item.children&&item.children.length>0){
                         // @ts-ignore
                         const temp = generateRouter(item.children,[],[...item.parentKey,...[item.path]]);
                         item.children = temp

                         tree.push(item)
                     }else {
                         tree.push(item)
                     }
                     if(item.path&&item.type==1){
                         let routerObj =  {
                             path:item.path?'/'+item.path:'',
                             name:item.path?.replaceAll('/', '-'),
                             meta:{
                                 title:item.name,
                                 icon:item.icon
                             },

                         }

                         routerObj['component'] = modules[`/src/views/${item.path||''}.vue`]
                         // console.log(routerObj)
                         // @ts-ignore
                         if(routerObj.component){
                             // @ts-ignore
                             router.addRoute('home',routerObj)
                         }


                     }
                 })
                 return tree;
             }
             queryMenuTree().then(res=>{
                 // @ts-ignore
                 const treeList =  generateRouter(res.result.menuTree,[]);
                 // @ts-ignore
                 localStorage.setItem('permissionList',JSON.stringify(res.result.permissionList))
                 localStorage.setItem('menuTree',JSON.stringify(treeList))
                 menuTree.value = treeList
                 // @ts-ignore



             })
         }
         const setCollapse = (val:boolean)=>{
            collapsed.value = val;
         }
         const logOut = (router)=>{
             exitLogin().then(res=>{
                 console.log(res)
                 message.success('操作成功');
                 localStorage.clear();
                 sessionStorage.clear();
                 router.replace('/')
             })
         }
        return { userInfo,routerKey,openKey,menuTree,
            collapsed,uploadQueue,rememberMe,
            setUserInfo,setRouterKey,setOpenKey,
            setMenuTree ,refreshMenu,setCollapse,logOut,
            setUploadQueue,setRememberMe
        }
    },
    {
        // @ts-ignore
        persist: {
            paths: ['userInfo', 'openKey','menuTree','collapsed','routerKey','rememberMe'],
        },

    })
export default  useSystemStore;
